// pages/mine/mySize/mySize.js
const app = getApp();
const util = require("../../../utils/util.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    sizeList: [],
    selected: false,

  },

  toNewSize: function(e) {
    // 判断是否绑定手机号
    let phone = wx.getStorageSync('phone')
    if (phone.length == 11) {
      wx.navigateTo({
        url: 'newSize/newSize'
      })
    } else {
      wx.showModal({
        title: '请先绑定手机号',
        showCancel: false
      })
    }
  },

  // 选择尺寸
  next: function (e) {
    let that = this
    if (that.data.sizeId != undefined && that.data.productId != undefined){
      wx.navigateTo({
        url: '../address/address?productId=' + that.data.productId + '&sizeId=' + that.data.sizeId
      })
    }
  },

  // 点击编辑按钮
  toCompileSize: function(e) {
    // console.log(e.target.id)
    let sizeId = e.target.id
    wx.navigateTo({
      url: 'compileSize/compileSize?sizeId=' + sizeId
    })
  },

  // 点击删除按钮
  deleteSize: function(e) {
    let that = this
    // console.log(e)
    let sizeId = e.target.id
    wx.showModal({
      title: '确认删除',
      content: '您确认删除该尺寸吗',
      confirmText: "确认",
      cancelText: "取消",
      success: function (res) {
        // console.log(res);
        if (res.confirm) {
          // 发送删除尺寸请求
          let deleteUrl = app.globalData.baseUrl + "weixin/deleteSize"
          let data = {
            sizeId: sizeId
          }
          util.requestLoading(deleteUrl, data, "GET").then(res => {
            // console.log(res)
            // 请求尺寸列表
            let sizeUrl = app.globalData.baseUrl + "weixin/sizeList"
            let cid = wx.getStorageSync('cid')
            let data = {
              cid: cid
            }
            util.requestLoading(sizeUrl, data, "GET").then(res => {
              // console.log(res)
              for (var i = 0; i < res.data.sizeList.length; i++) {
                var aaa = JSON.parse(res.data.sizeList[i].optionMsg)
                res.data.sizeList[i].optionMsg = aaa
              }
              that.setData({
                sizeList: res.data.sizeList,
                next:0
              })
            })
          })
        }
      }
    })
  },
  //选中对应的item
  selectedItem:function(res){
     
    let sizeList = this.data.sizeList
    console.log(res.currentTarget.dataset.id)
    for(var i =0;i<sizeList.length;i++){
      sizeList[i].selected = false
    }
    sizeList[res.currentTarget.dataset.id].selected = true

    this.setData({
      sizeList: sizeList,
      next:1,
      sizeId: sizeList[res.currentTarget.dataset.id].sizeId
    })
 

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    let productId = options.productId
    if (productId!=undefined){
      that.setData({
        productId: options.productId,
        redio: 1
      })
    }
    console.log(that.data.productId)
    // 请求尺寸列表
    let sizeUrl = app.globalData.baseUrl + "weixin/sizeList"
    let cid = wx.getStorageSync('cid')
    let data = {
      cid: cid
    }
    util.requestLoading(sizeUrl, data, "GET").then(res => {
      // console.log(res)
      for (var i = 0; i < res.data.sizeList.length; i++) {
        var aaa = JSON.parse(res.data.sizeList[i].optionMsg)
        res.data.sizeList[i].optionMsg = aaa
        res.data.sizeList[i].selected = false
      }
      that.setData({
        sizeList: res.data.sizeList,
        next:0
      })
      console.log(that.data.sizeList)
    })
  },
  onShow: function(e) {
    let that = this
    // console.log(that.data.productId)
    // 请求尺寸列表
    let sizeUrl = app.globalData.baseUrl + "weixin/sizeList"
    let cid = wx.getStorageSync('cid')
    let data = {
      cid: cid
    }
    util.requestLoading(sizeUrl, data, "GET").then(res => {
      console.log(res)
      for (var i = 0; i < res.data.sizeList.length;i++){
        var aaa = JSON.parse(res.data.sizeList[i].optionMsg)
        res.data.sizeList[i].optionMsg = aaa
        res.data.sizeList[i].selected = false
      }
      
      that.setData({
        sizeList: res.data.sizeList,
        next:0
      })
    })
  },
})